package com.pig4cloud.pig.module.controller.estate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.estate.EstateRoom;
import com.pig4cloud.pig.module.api.entity.estate.EstateRoomBangUser;
import com.pig4cloud.pig.module.service.estate.IEstateRoomBangUserService;
import com.pig4cloud.pig.module.service.estate.IEstateRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 小区房屋绑定微信用户Controller
 *
 * @author ruoyi
 * @date 2023-08-19
 */
@Validated
@Api(value = "小区房屋绑定微信用户控制器", tags = {"小区房屋绑定微信用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/estateUserBang")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EstateRoomBangUserController {

    private final IEstateRoomBangUserService estateRoomBangUserService;
    private final IEstateRoomService estateRoomService;

    /**
     * 查询小区房屋绑定微信用户列表
     */
    @ApiOperation("查询小区房屋绑定微信用户列表")
    @PreAuthorize("@pms.hasPermission('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateRoomBangUser> list(@Validated EstateRoomBangUser estateRoomBangUser) {
        QueryWrapper<EstateRoomBangUser> qw = new QueryWrapper<>();
        if (estateRoomBangUser.getBangWxUserId()!=null){
            qw.eq("bang_wx_user_id",estateRoomBangUser.getBangWxUserId());
        }
        TableDataInfo<EstateRoomBangUser> td = PageUtils.buildDataInfo(estateRoomBangUserService.page(PageUtils.buildPage(),qw));
        List<EstateRoomBangUser> list = td.getData();
        return td;
    }

    /**
     * 导出小区房屋绑定微信用户列表
     */
    @ApiOperation("导出小区房屋绑定微信用户列表")
    @PreAuthorize("@pms.hasPermission('system:user:export')")
    @SysLog("小区房屋绑定微信用户")
    @GetMapping("/export")
    public List export(@Validated EstateRoomBangUser estateRoomBangUser) {
        List<EstateRoomBangUser> list = estateRoomBangUserService.list();
//        // ExcelUtil<EstateRoomBangUser> util = new // ExcelUtil<EstateRoomBangUser>(EstateRoomBangUser.class);
        return list;// util.exportExcel(list, "小区房屋绑定微信用户");
    }

    /**
     * 获取小区房屋绑定微信用户详细信息
     */
    @ApiOperation("获取小区房屋绑定微信用户详细信息")
    @PreAuthorize("@pms.hasPermission('system:user:query')")
    @GetMapping("/{id}")
    public R<EstateRoomBangUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateRoomBangUser estateRoomBangUser = estateRoomBangUserService.getById(id);
        return R.ok(estateRoomBangUser);
    }

    /**
     * 新增小区房屋绑定微信用户
     */
    @ApiOperation("新增小区房屋绑定微信用户")
    @PreAuthorize("@pms.hasPermission('module:estateBangUser:add')")
    @SysLog("小区房屋绑定微信用户")
    @PostMapping()
    public R<Void> add(@Valid @RequestBody EstateRoomBangUser estateRoomBangUser) {
        QueryWrapper<EstateRoom> qw = new QueryWrapper<>();
        if(estateRoomBangUser.getEstateId()!=null){
            qw.eq("estate_id",estateRoomBangUser.getEstateId());
        }
        List<EstateRoom> list = estateRoomService.list(qw);
        for (EstateRoom list1:list){
            EstateRoomBangUser estateRoomBangUser1 = new EstateRoomBangUser();
            estateRoomBangUser1.setEstateId(estateRoomBangUser.getEstateId());
            estateRoomBangUser1.setBangWxUserId(estateRoomBangUser.getBangWxUserId());
            estateRoomBangUser1.setRoomId(list1.getId());
            boolean b = estateRoomBangUserService.save(estateRoomBangUser1);
            if (b==false){
                return R.okByRow(false);
            }
        }
        return R.okByRow(true);
    }

    /**
     * 修改小区房屋绑定微信用户
     */
    @ApiOperation("修改小区房屋绑定微信用户")
    @PreAuthorize("@pms.hasPermission('system:user:edit')")
    @SysLog("小区房屋绑定微信用户")
    @PutMapping()
    public R<Void> edit(@Valid@RequestBody EstateRoomBangUser estateRoomBangUser) {
        return R.okByRow(estateRoomBangUserService.updateById(estateRoomBangUser) ? 1 : 0);
    }

    /**
     * 删除小区房屋绑定微信用户
     */
    @ApiOperation("删除小区房屋绑定微信用户")
    @PreAuthorize("@pms.hasPermission('system:user:remove')")
    @SysLog("小区房屋绑定微信用户")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(estateRoomBangUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
